<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Operations with images</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../de/d7a/tutorial_table_of_content_core.html">The Core Functionality (core module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Operations with images </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d7/d37/tutorial_mat_mask_operations.html">Mask operations on matrices</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d5/dc4/tutorial_adding_images.html">Adding (blending) two images using OpenCV</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Input/Output </h2>
<h3>Images</h3>
<p>Load an image from a file:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        Mat img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(filename);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            Mat img = Imgcodecs.imread(filename);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(filename)</div></div><!-- fragment --> </div> <p>If you read a jpg file, a 3 channel image is created by default. If you need a grayscale image, use:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        Mat img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(filename, <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80ae29981cfc153d3b0cef5c0daeedd2125">IMREAD_GRAYSCALE</a>);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            Mat img = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(filename, cv.IMREAD_GRAYSCALE)</div></div><!-- fragment --> </div> <dl class="section note"><dt>Note</dt><dd>Format of the file is determined by its content (first few bytes). To save an image to a file:</dd></dl>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">imwrite</a>(filename, img);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            Imgcodecs.imwrite(filename, img);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce">cv.imwrite</a>(filename, img)</div></div><!-- fragment --> </div> <dl class="section note"><dt>Note</dt><dd>Format of the file is determined by its extension.</dd>
<dd>
Use <a class="el" href="../../d4/da8/group__imgcodecs.html#ga26a67788faa58ade337f8d28ba0eb19e" title="Reads an image from a buffer in memory. ">cv::imdecode</a> and <a class="el" href="../../d4/da8/group__imgcodecs.html#ga461f9ac09887e47797a54567df3b8b63" title="Encodes an image into a memory buffer. ">cv::imencode</a> to read and write an image from/to memory rather than a file.</dd></dl>
<h2>Basic operations with images </h2>
<h3>Accessing pixel intensity values</h3>
<p>In order to get pixel intensity value, you have to know the type of an image and the number of channels. Here is an example for a single channel grey scale image (type 8UC1) and pixel coordinates x and y:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a> intensity = img.at&lt;<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a>&gt;(y, x);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">                byte[] imgData = <span class="keyword">new</span> byte[(int) (img.total() * img.channels())];</div><div class="line">                img.get(0, 0, imgData);</div><div class="line">                byte intensity = imgData[y * img.cols() + x];</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    _intensity = img[y,x]</div></div><!-- fragment --> </div> <p>C++ version only: intensity.val[0] contains a value from 0 to 255. Note the ordering of x and y. Since in OpenCV images are represented by the same structure as matrices, we use the same convention for both cases - the 0-based row index (or y-coordinate) goes first and the 0-based column index (or x-coordinate) follows it. Alternatively, you can use the following notation (<b>C++ only</b>):</p>
<div class="fragment"><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a> intensity = img.at&lt;<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a>&gt;(<a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(x, y));</div></div><!-- fragment --><p> Now let us consider a 3 channel image with BGR color ordering (the default format returned by imread):</p>
<p><b>C++ code</b> </p><div class="fragment"><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a> intensity = img.at&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga7e6060c0b8d48459964df6e1eb524c03">Vec3b</a>&gt;(y, x);</div><div class="line">            <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a> blue = intensity.val[0];</div><div class="line">            <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a> green = intensity.val[1];</div><div class="line">            <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a> red = intensity.val[2];</div></div><!-- fragment --><p> <b>Python Python</b> </p><div class="fragment"><div class="line">    _blue = img[y,x,0]</div><div class="line">    _green = img[y,x,1]</div><div class="line">    _red = img[y,x,2]</div></div><!-- fragment --><p> You can use the same method for floating-point images (for example, you can get such an image by running Sobel on a 3 channel image) (<b>C++ only</b>):</p>
<div class="fragment"><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a> intensity = img.at&lt;<a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>&gt;(y, x);</div><div class="line">            <span class="keywordtype">float</span> blue = intensity.<a class="code" href="../../de/de1/classcv_1_1Matx.html#afa9ad3bc5b09ebcdfc6f98f44c15191d">val</a>[0];</div><div class="line">            <span class="keywordtype">float</span> green = intensity.val[1];</div><div class="line">            <span class="keywordtype">float</span> red = intensity.val[2];</div></div><!-- fragment --><p> The same method can be used to change pixel intensities:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">            img.at&lt;<a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a>&gt;(y, x) = 128;</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">                byte[] imgData = <span class="keyword">new</span> byte[(int) (img.total() * img.channels())];</div><div class="line">                imgData[y * img.cols() + x] = (byte) 128;</div><div class="line">                img.put(0, 0, imgData);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img[y,x] = 128</div></div><!-- fragment --> </div> <p>There are functions in OpenCV, especially from calib3d module, such as <a class="el" href="../../d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c" title="Projects 3D points to an image plane. ">cv::projectPoints</a>, that take an array of 2D or 3D points in the form of Mat. Matrix should contain exactly one column, each row corresponds to a point, matrix type should be 32FC2 or 32FC3 correspondingly. Such a matrix can be easily constructed from <code>std::vector</code> (<b>C++ only</b>):</p>
<div class="fragment"><div class="line">            vector&lt;Point2f&gt; points;</div><div class="line">            <span class="comment">//... fill the array</span></div><div class="line">            Mat pointsMat = Mat(points);</div></div><!-- fragment --><p> One can access a point in this matrix using the same method <code>Mat::at</code> (<b>C++ only</b>):</p>
<div class="fragment"><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga7d080aa40de011e4410bca63385ffe2a">Point2f</a> point = pointsMat.at&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga7d080aa40de011e4410bca63385ffe2a">Point2f</a>&gt;(i, 0);</div></div><!-- fragment --> <h3>Memory management and reference counting</h3>
<p>Mat is a structure that keeps matrix/image characteristics (rows and columns number, data type etc) and a pointer to data. So nothing prevents us from having several instances of Mat corresponding to the same data. A Mat keeps a reference count that tells if data has to be deallocated when a particular instance of Mat is destroyed. Here is an example of creating two matrices without copying data (<b>C++ only</b>):</p>
<div class="fragment"><div class="line">        std::vector&lt;Point3f&gt; points;</div><div class="line">        <span class="comment">// .. fill the array</span></div><div class="line">        Mat pointsMat = Mat(points).reshape(1);</div></div><!-- fragment --><p> As a result, we get a 32FC1 matrix with 3 columns instead of 32FC3 matrix with 1 column. <code>pointsMat</code> uses data from points and will not deallocate the memory when destroyed. In this particular instance, however, developer has to make sure that lifetime of <code>points</code> is longer than of <code>pointsMat</code> If we need to copy the data, this is done using, for example, <a class="el" href="../../d3/d63/classcv_1_1Mat.html#a33fd5d125b4c302b0c9aa86980791a77" title="Copies the matrix to another one. ">cv::Mat::copyTo</a> or <a class="el" href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660" title="Creates a full copy of the array and the underlying data. ">cv::Mat::clone</a>:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        Mat img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<span class="stringliteral">&quot;image.jpg&quot;</span>);</div><div class="line">        Mat img1 = img.clone();</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            Mat img = Imgcodecs.imread(<span class="stringliteral">&quot;image.jpg&quot;</span>);</div><div class="line">            Mat img1 = img.clone();</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<span class="stringliteral">&#39;image.jpg&#39;</span>)</div><div class="line">    _img1 = np.copy(img)</div></div><!-- fragment --> </div> <p>An empty output Mat can be supplied to each function. Each implementation calls Mat::create for a destination matrix. This method allocates data for a matrix if it is empty. If it is not empty and has the correct size and type, the method does nothing. If however, size or type are different from the input arguments, the data is deallocated (and lost) and a new data is allocated. For example:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        Mat img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<span class="stringliteral">&quot;image.jpg&quot;</span>);</div><div class="line">        Mat sobelx;</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(img, sobelx, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, 1, 0);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            Mat img = Imgcodecs.imread(<span class="stringliteral">&quot;image.jpg&quot;</span>);</div><div class="line">            Mat sobelx = <span class="keyword">new</span> Mat();</div><div class="line">            Imgproc.Sobel(img, sobelx, CvType.CV_32F, 1, 0);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<span class="stringliteral">&#39;image.jpg&#39;</span>)</div><div class="line">    _sobelx = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">cv.Sobel</a>(img, cv.CV_32F, 1, 0)</div></div><!-- fragment --> </div> <h3>Primitive operations</h3>
<p>There is a number of convenient operators defined on a matrix. For example, here is how we can make a black image from an existing greyscale image <code>img</code></p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">            img = <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">                byte[] imgData = <span class="keyword">new</span> byte[(int) (img.total() * img.channels())];</div><div class="line">                Arrays.fill(imgData, (byte) 0);</div><div class="line">                img.put(0, 0, imgData);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img[:] = 0</div></div><!-- fragment --> </div> <p>Selecting a region of interest:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a> r(10, 10, 100, 100);</div><div class="line">            Mat smallImg = img(r);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">                <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a> r = <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(10, 10, 100, 100);</div><div class="line">                Mat smallImg = img.submat(r);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    _smallImg = img[10:110,10:110]</div></div><!-- fragment --> </div> <p>Conversion from color to greyscale:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        Mat img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<span class="stringliteral">&quot;image.jpg&quot;</span>); <span class="comment">// loading a 8UC3 image</span></div><div class="line">        Mat grey;</div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(img, grey, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a>);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            Mat img = Imgcodecs.imread(<span class="stringliteral">&quot;image.jpg&quot;</span>); <span class="comment">// loading a 8UC3 image</span></div><div class="line">            Mat grey = <span class="keyword">new</span> Mat();</div><div class="line">            Imgproc.cvtColor(img, grey, Imgproc.COLOR_BGR2GRAY);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<span class="stringliteral">&#39;image.jpg&#39;</span>)</div><div class="line">    _grey = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(img, cv.COLOR_BGR2GRAY)</div></div><!-- fragment --> </div> <p>Change image type from 8UC1 to 32FC1:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        src.convertTo(dst, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            src.convertTo(dst, CvType.CV_32F);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    _dst = src.astype(np.float32)</div></div><!-- fragment --> </div> <h3>Visualizing images</h3>
<p>It is very useful to see intermediate results of your algorithm during development process. OpenCV provides a convenient way of visualizing images. A 8U image can be shown using:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        Mat img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<span class="stringliteral">&quot;image.jpg&quot;</span>);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(<span class="stringliteral">&quot;image&quot;</span>, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a>);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;image&quot;</span>, img);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>();</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            Mat img = Imgcodecs.imread(<span class="stringliteral">&quot;image.jpg&quot;</span>);</div><div class="line">            HighGui.namedWindow(<span class="stringliteral">&quot;image&quot;</span>, HighGui.WINDOW_AUTOSIZE);</div><div class="line">            HighGui.imshow(<span class="stringliteral">&quot;image&quot;</span>, img);</div><div class="line">            HighGui.waitKey();</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<span class="stringliteral">&#39;image.jpg&#39;</span>)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(<span class="stringliteral">&#39;image&#39;</span>, cv.WINDOW_AUTOSIZE)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;image&#39;</span>, img)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>()</div></div><!-- fragment --> </div> <p>A call to <a class="el" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7" title="Waits for a pressed key. ">waitKey()</a> starts a message passing cycle that waits for a key stroke in the "image" window. A 32F image needs to be converted to 8U type. For example:</p>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        Mat img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<span class="stringliteral">&quot;image.jpg&quot;</span>);</div><div class="line">        Mat grey;</div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(img, grey, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a>);</div><div class="line">        Mat sobelx;</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(grey, sobelx, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>, 1, 0);</div><div class="line">        <span class="keywordtype">double</span> minVal, maxVal;</div><div class="line">        <a class="code" href="../../d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707">minMaxLoc</a>(sobelx, &amp;minVal, &amp;maxVal); <span class="comment">//find minimum and maximum intensities</span></div><div class="line">        Mat draw;</div><div class="line">        sobelx.convertTo(draw, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32b18d904ee2b1731a9416a8eef67d06">CV_8U</a>, 255.0/(maxVal - minVal), -minVal * 255.0/(maxVal - minVal));</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(<span class="stringliteral">&quot;image&quot;</span>, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a>);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;image&quot;</span>, draw);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>();</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">            Mat img = Imgcodecs.imread(<span class="stringliteral">&quot;image.jpg&quot;</span>);</div><div class="line">            Mat grey = <span class="keyword">new</span> Mat();</div><div class="line">            Imgproc.cvtColor(img, grey, Imgproc.COLOR_BGR2GRAY);</div><div class="line">            Mat sobelx = <span class="keyword">new</span> Mat();</div><div class="line">            Imgproc.Sobel(grey, sobelx, CvType.CV_32F, 1, 0);</div><div class="line">            MinMaxLocResult res = Core.minMaxLoc(sobelx); <span class="comment">// find minimum and maximum intensities</span></div><div class="line">            Mat draw = <span class="keyword">new</span> Mat();</div><div class="line">            <span class="keywordtype">double</span> maxVal = res.maxVal, minVal = res.minVal;</div><div class="line">            sobelx.convertTo(draw, CvType.CV_8U, 255.0 / (maxVal - minVal), -minVal * 255.0 / (maxVal - minVal));</div><div class="line">            HighGui.namedWindow(<span class="stringliteral">&quot;image&quot;</span>, HighGui.WINDOW_AUTOSIZE);</div><div class="line">            HighGui.imshow(<span class="stringliteral">&quot;image&quot;</span>, draw);</div><div class="line">            HighGui.waitKey();</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<span class="stringliteral">&#39;image.jpg&#39;</span>)</div><div class="line">    grey = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(img, cv.COLOR_BGR2GRAY)</div><div class="line">    sobelx = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">cv.Sobel</a>(grey, cv.CV_32F, 1, 0)</div><div class="line">    <span class="comment"># find minimum and maximum intensities</span></div><div class="line">    minVal = np.amin(sobelx)</div><div class="line">    maxVal = np.amax(sobelx)</div><div class="line">    draw = <a class="code" href="../../d2/de8/group__core__array.html#ga3460e9c9f37b563ab9dd550c4d8c4e7d">cv.convertScaleAbs</a>(sobelx, alpha=255.0/(maxVal - minVal), beta=-minVal * 255.0/(maxVal - minVal))</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(<span class="stringliteral">&#39;image&#39;</span>, cv.WINDOW_AUTOSIZE)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(<span class="stringliteral">&#39;image&#39;</span>, draw)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>()</div></div><!-- fragment --> </div> <dl class="section note"><dt>Note</dt><dd>Here <a class="el" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b" title="Creates a window. ">cv::namedWindow</a> is not necessary since it is immediately followed by <a class="el" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563" title="Displays an image in the specified window. ">cv::imshow</a>. Nevertheless, it can be used to change the window properties or when using <a class="el" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b" title="Creates a trackbar and attaches it to the specified window. ">cv::createTrackbar</a> </dd></dl>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:34 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
